﻿using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using Medianamik.Core;
using Medianamik.Core.Configuration;
using NUnit.Framework;

namespace Medianamik.Tests.Integration.Medianamik.Core.Configuration
{
    [TestFixture]
    public class MedianamikConfigSectionTest
    {
        [Test]
        public void MaxUploadfileSize_should_be_equal_to_value_specified_in_config_file()
        {
            var doc = XDocument.Load("App.config");
            int expectedMaxUploadFileSize = GetExpectedMaxUploadFileSize(doc);
            var config = (MedianamikConfigSection)ConfigurationManager
                                                      .GetSection(ConfigurationSectionName.MEDIANAMIK);


            var actualMaxUploadFileSize = config.EditorMaxUploadFileSize;
            
            Assert.That(actualMaxUploadFileSize, Is.EqualTo(expectedMaxUploadFileSize));
        }

        private int GetExpectedMaxUploadFileSize(XDocument doc)
        {
            var maxUploadAttribute = doc.Root
                .Descendants("Medianamik")
                .Single()
                .Attribute(ConfigurationPropertyName.EDITOR_MAX_UPLOAD_FILE_SIZE);

            return int.Parse(maxUploadAttribute.Value);
        }
    }
}